package com.learn.russianRoulette;
import com.learn.russianRoulette.entity.gun;
import com.learn.russianRoulette.entity.role;
import com.learn.russianRoulette.service.gameService;

import java.util.Arrays;
/**
 * 赌博游戏，勇气与运气的对抗
 */
public class Main {
    /**
     * 14：30对远程分支更新
     * 分支合并的测试
     * 主程序入口
     * 模拟一个游戏过程，玩家需要通过多轮射击游戏来获胜
     * 每轮游戏使用不同的枪支和子弹配置
     */
    public static void main(String[] args) {
        // 初始化玩家和第一个对手的角色
        role role1 = new role();
        role roleBoss = new role();
        // 初始化游戏服务
        gameService gameService = new gameService();
        // 开始游戏
        gameService.gameStart(role1, roleBoss);
        // 用于记录通过的关卡数
        int passRound = 0;
        while(true){
            // 设置对手的角色名称和生命值
            roleBoss.setRoleName("对方");
            roleBoss.setHealth(1);
            // 创建第一把枪，设定子弹数量和最大容量
            gun gun1 = new gun(1, 3);
            // 初始化子弹数组
            int[] bullets = gun1.gunSet(gun1.getBulletCount(), gun1.getBulletCountMax());
            // 打印子弹数组（调试用）
//        System.out.println(Arrays.toString(bullets));
            // 初始化射击轮次
            int i = 0;
            // 打印当前枪支的子弹配置信息
            System.out.println("\n现在，桌子上有一把霰弹枪，一共有" + gun1.getBulletCountMax() + "发子弹，其中 实弹" +
                    gun1.getBulletCount() + "发，空弹"+(gun1.getBulletCountMax()-gun1.getBulletCount())+"发");
            // 决定谁先开枪
            int firstFlag = gameService.whoFirst();
            // 根据谁先开枪的结果，进行射击
            switch (firstFlag){
                case 0:
                    // 玩家先开枪
                    int flag1 = gameService.shotRole1(role1, roleBoss, bullets, i);
                    // 循环射击直到一方生命值为0
                    while(role1.getHealth() > 0&& roleBoss.getHealth() > 0){
                        switch (flag1){
                            case 0:
                                flag1 = gameService.shotRole1(role1, roleBoss, bullets, ++i);
                                break;
                            case 1:
                                flag1 = gameService.shotBoss(roleBoss, role1, bullets, ++i);
                                break;
                        }
                    }
                    break;
                case 1:
                    // 对手先开枪
                    int flag2 = gameService.shotBoss(roleBoss, role1, bullets, i);
                    // 循环射击直到一方生命值为0
                    while(role1.getHealth() > 0 && roleBoss.getHealth() > 0){
                        switch (flag2){
                            case 0:
                                flag2 = gameService.shotRole1(role1, roleBoss, bullets, ++i);
                                break;
                            case 1:
                                flag2 = gameService.shotBoss(roleBoss, role1, bullets, ++i);
                                break;
                        }
                    }
                    break;
            }
            // 如果玩家生命值为0，游戏结束，记录通过的关卡数
            if (role1.getHealth() == 0){
                passRound = 1;
                break;
            }
            // 玩家幸运逃脱，准备下一轮游戏
            else{
                System.out.println("运气不错，准备面对下一位死刑犯吧");
            }

            // 重复上述流程
            role roleBoss2 = new role();
            roleBoss2.setRoleName("对方");
            roleBoss2.setHealth(1);
            gun gun2 = new gun(3, 5);
            int[] bullets2 = gun2.gunSet(gun2.getBulletCount(), gun2.getBulletCountMax());
            int j = 0;
            System.out.println("\n这次的霰弹枪，一共有" + gun2.getBulletCountMax() + "发子弹，其中 实弹" +
                    gun2.getBulletCount() + "发，空弹"+(gun2.getBulletCountMax()-gun2.getBulletCount())+"发");
            int firstFlag2 = gameService.whoFirst();
            switch (firstFlag2){
                case 0:
                    int flag1 = gameService.shotRole1(role1, roleBoss2, bullets2, j);
                    while(role1.getHealth() > 0&& roleBoss2.getHealth() > 0){
                        switch (flag1){
                            case 0:
                                flag1 = gameService.shotRole1(role1, roleBoss2, bullets2, ++j);
                                break;
                            case 1:
                                flag1 = gameService.shotBoss(roleBoss2, role1, bullets2, ++j);
                                break;
                        }
                    }
                    break;
                case 1:
                    int flag2 = gameService.shotBoss(roleBoss2, role1, bullets2, j);
                    while(role1.getHealth() > 0 && roleBoss2.getHealth() > 0){
                        switch (flag2){
                            case 0:
                                flag2 = gameService.shotRole1(role1, roleBoss2, bullets2, ++j);
                                break;
                            case 1:
                                flag2 = gameService.shotBoss(roleBoss2, role1, bullets2, ++j);
                                break;
                        }
                    }
                    break;
            }
            if (role1.getHealth() == 0){
                passRound = 2;
                break;
            }
            else{
                System.out.println("运气不错，准备面对下一位死刑犯吧");
            }

            role roleBoss3 = new role();
            roleBoss3.setRoleName("对方");
            roleBoss3.setHealth(1);
            gun gun3 = new gun(1, 6);
            int[] bullets3 = gun3.gunSet(gun3.getBulletCount(), gun3.getBulletCountMax());
            int k = 0;
            System.out.println("\n没想到你居然赢到了现在，最后一轮采用非常经典的俄罗斯转盘\n这有一把左轮手枪，一共有" + gun3.getBulletCountMax() + "发子弹，其中 实弹" +
                    gun3.getBulletCount() + "发，空弹"+(gun3.getBulletCountMax()-gun3.getBulletCount())+"发");
            int firstFlag3 = gameService.whoFirst();
            switch (firstFlag3){
                case 0:
                    int flag1 = gameService.shotRole1(role1, roleBoss3, bullets3, k);
                    while(role1.getHealth() > 0&& roleBoss3.getHealth() > 0){
                        switch (flag1){
                            case 0:
                                flag1 = gameService.shotRole1(role1, roleBoss3, bullets3, ++k);
                                break;
                            case 1:
                                flag1 = gameService.shotBoss(roleBoss3, role1, bullets3, ++k);
                                break;
                        }
                    }
                    break;
                case 1:
                    int flag2 = gameService.shotBoss(roleBoss3, role1, bullets3, k);
                    while(role1.getHealth() > 0 && roleBoss3.getHealth() > 0){
                        switch (flag2){
                            case 0:
                                flag2 = gameService.shotRole1(role1, roleBoss3, bullets3, ++k);
                                break;
                            case 1:
                                flag2 = gameService.shotBoss(roleBoss3, role1, bullets3, ++k);
                                break;
                        }
                    }
                    break;
            }
            if (role1.getHealth() == 0){
                passRound = 3;
            }
            break;
        }
        // 根据玩家生命值和通过的关卡数，打印游戏结果
        if(role1.getHealth() > 0){
            System.out.println("真没想到，你居然赢了");
            pauseForOneSecond(1.0);
            System.out.println("只可惜这不过是直播给富人看的赌徒游戏罢了\n你刚才杀死了三个和你一样无辜的可怜人，现在你是不折不扣的死刑犯了，哈哈");
        }else{
            switch (passRound){
                case 1:
                    System.out.println("真不走运，你连第一关都没有过");
                    break;
                case 2:
                    System.out.println("你倒在了第二个杀人犯手里，不走运的家伙");
                    break;
                case 3:
                    System.out.println("好可惜，差一点就能获得自由了");
                    break;
            }
        }
    }

    public static void pauseForOneSecond(double second) {
        // 将秒转换为毫秒，因为Thread.sleep方法需要毫秒参数
        second = second * 1000;
        try {
            // 使当前线程睡眠指定的毫秒数
            Thread.sleep((int)second);
        } catch (InterruptedException e) {
            // 当线程被中断时，捕获InterruptedException并打印堆栈跟踪
            e.printStackTrace();
        }
    }
}




